﻿#include <iostream>
#include "decoupler.h"




CFensDecoupler::CFensDecoupler(const string &startStr, const string &endStr):
m_startStr(startStr), m_endStr(endStr)
{
	//cout <<"startstr: "<<m_startStr<<", endStr: "<<endStr<<endl;;
	//cout <<"fens decoupler init!"<<endl;
}


CFensDecoupler::~CFensDecoupler()
{

}

void CFensDecoupler::Put(const string &data )
{
	m_data += data;
}


int CFensDecoupler::Get( string &data )
{
ReFind:    
    size_t startPos = m_data.find(m_startStr);
    if ( startPos == string::npos )
    {
        return -1;
    }
	
    size_t endPos = m_data.find(m_endStr, startPos+m_startStr.size());
    if ( endPos == string::npos )
    {
        return -2;
    }

    int dataLen = (endPos-startPos)+m_endStr.size();
	if( ((unsigned int)dataLen == (m_startStr.size() * 2) ) && (m_endStr == m_startStr) ) //开始结束标志相同时，有时前一帧数据不完整，丢失开始字符时，需要特殊处理
	{
		m_data = m_data.substr(startPos+m_startStr.size()); //跳过这个被认为是开始符的结束符
		goto ReFind;
	}
	
    data = m_data.substr(startPos, dataLen);

    m_data = m_data.substr(endPos+m_endStr.size());

    //cout <<"now m_data: "<<endl;
    //debug_print(m_data.data(), m_data.size());

    return 0;
}





























